package cn.felord.app.config;


import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger2 配置
 *
 * @author dax
 * @since 20190328
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(value = "swagger2.enabled")
public class Swagger2Config {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("小程序api")
                .apiInfo(miniAppApi())
                .globalOperationParameters(jwtToken())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.felord"))
                .paths(PathSelectors.any())
                .build();
    }


    //    @Bean
    public Docket backApp() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("管理后台api")
                .apiInfo(backupAppApi())
                .globalOperationParameters(jwtToken())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.oox.manage.api"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 小程序api
     *
     * @return api info
     */
    private ApiInfo miniAppApi() {
        return new ApiInfoBuilder()
                .title("小程序api")
                .description("接口文档")
                .version("0.1.0")
                .build();
    }

    /**
     * 管理后台api
     *
     * @return api info
     */
    private ApiInfo backupAppApi() {
        return new ApiInfoBuilder()
                .title("管理后台api")
                .description("接口文档")
                .version("0.1.0")
                .build();
    }


    private List<Parameter> jwtToken() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("Authorization").description("jwt令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }
}
